﻿using ALMDDD.Domain.Core.Bus;
using ALMDDD.Domain.Core.Commands;
using ALMDDD.Domain.Core.Notifications;
using ALMDDD.Domain.Interfaces;
using System;
using System.Collections.Generic;
using System.Text;

namespace ALMDDD.Domain.CommandHandlers
{
    /// <summary>
    /// 领域命令处理程序
    /// </summary>
   public class CommandHandler
    {
        /// <summary>
        /// 注入工作单元
        /// </summary>
        private readonly IunitOfWork _unit;
        /// <summary>
        /// 注入中介处理程序接口
        /// </summary>
        private readonly IMeDiatorHandler _bus;

        protected CommandHandler(IunitOfWork unit, IMeDiatorHandler bus)
        {
            _unit = unit;
            _bus = bus;
        }
        public bool Commit()
        {
            if (_unit.Commit()) return true;
            return false;
        }
        protected void NotifyValidationErrors(Command message)
        {
            List<string> errorInofo = new List<string>();
            foreach (var item in message.ValidationResult.Errors)
            {
                //将错误信息提交到事件总线，派发出去
                _bus.RaiseEvent(new DomainNotification("",item.ErrorMessage));

            }
        }

    }
}
